const { regeneratorRuntime, throttle, dayjs } = global

Page(global.connect(
  state => ({
    ...state.globalReducer
  }),
  dispatch => ({
    initHome(data) {
      dispatch(global.globalReducer.initGlobal(data))
    },
    showSidebar(data) {
      dispatch(global.globalReducer.showSidebar(data))
    },
    hideSidebar(data) {
      dispatch(global.globalReducer.hideSidebar(data))
    },
    // 审核员最新活动列表
    async getAuditLatestActivityList(refresh) {
      try {
        // console.log('getAuditProjectList')
        if (!this.uiLoading1) {
          // step1
          this.setData({ uiLoading1: true })

          // step2
          const { uiCurrentPage1, uiPageSize1, uiKeyword1, uiList1 } = this.data
          const nextPage = uiCurrentPage1 + 1
          const condition = {
            id: this.data.minaLoginRes.data.user_id,
            page: nextPage,
            size: uiPageSize1,
            keyword: uiKeyword1,
            type: 1
          }
          const actionRes = await dispatch(global.activityReducer.getAuditLatestActivityList(condition))
          console.log(actionRes)
          if (actionRes.actionStatus === 'SUCCESS') {
            this.setData({
              uiCurrentPage1: nextPage,
              uiList1: refresh ? actionRes.data : [...uiList1, ...actionRes.data]
            })
          } else {
            this.selectComponent('#toast').handleShow({
              message: actionRes.apiMes
            })
            console.error(actionRes)
          }
          // step3
        }
      } catch (error) {
        console.error(error)
      }
      this.setData({ uiLoading1: false })
    },
    // 审核员历史活动列表
    async getAuditHistoryActivityList(refresh) {
      try {
        // console.log('getAuditProjectList')
        if (!this.uiLoading2) {
          // step1
          this.setData({ uiLoading2: true })

          // step2
          const { uiCurrentPage2, uiPageSize2, uiKeyword2, uiList2 } = this.data
          const nextPage = uiCurrentPage2 + 1
          const condition = {
            id: this.data.minaLoginRes.data.user_id,
            page: nextPage,
            size: uiPageSize2,
            keyword: uiKeyword2,
            type: 2
          }
          const actionRes = await dispatch(global.activityReducer.getAuditHistoryActivityList(condition))
          console.log(actionRes)
          if (actionRes.actionStatus === 'SUCCESS') {
            this.setData({
              uiCurrentPage2: nextPage,
              uiList2: refresh ? actionRes.data : [...uiList2, ...actionRes.data]
            })
          } else {
            this.selectComponent('#toast').handleShow({
              message: actionRes.apiMes
            })
            console.error(actionRes)
          }
          // step3
        }
      } catch (error) {
        console.error(error)
      }
      this.setData({ uiLoading2: false })
    },
  })
)({
  data: {
    uiTabIndex: 0,

    uiFnLock1: false,
    uiLoading1: false,
    uiList1: [],
    uiCurrentPage1: 0,
    uiPageSize1: 2,
    uiKeyword1: '',
    uiShowSearch1: false,


    uiFnLock2: false,
    uiLoading2: false,
    uiList2: [],
    uiCurrentPage2: 0,
    uiPageSize2: 2,
    uiKeyword2: '',
    uiShowSearch2: false
  },
  onLoad() {
    console.log('page activity onLoad')
  },
  onShow() {
    // console.log('index onshow')
    // todo
    global.waitUntilLogin(this.handleReloadListData)
    this.getAuditLatestActivityList(false)
    this.getAuditHistoryActivityList(false)
  },
  handleScrollLoad1(e) {
    this.getAuditLatestActivityList()
  },
  handleScrollLoad2(e) {
    this.getAuditHistoryActivityList()
  },
  handleActivityTap(e) {
    this.setData({
      uiTabIndex: e.currentTarget.dataset.index
    })
  },
  handleLicon1Tap(e) {
    console.log('handleLicon1Tap', e)
    wx.navigateBack()
  },
  handleRicon1Tap(e) {
    console.log('handleRicon1Tap', e)
    const { uiTabIndex, uiShowSearch1, uiShowSearch2 } = this.data;
    if (uiTabIndex === 0) {
      this.setData({ uiShowSearch1: !uiShowSearch1 })
    }
    if (uiTabIndex === 1) {
      this.setData({ uiShowSearch2: !uiShowSearch2 })
    }
  },
  handleSearchConfirm1(e) {
    console.log('handleSearchConfirm1', e, e.detail.detail.value)
    this.setData({ uiKeyword1: e.detail.detail.value })
    this.getAuditLatestActivityList(true)
  },
  handleSearchConfirm2(e) {
    console.log('handleSearchConfirm2', e, e.detail.detail.value)
    this.setData({ uiKeyword2: e.detail.detail.value })
    this.getAuditHistoryActivityList(true)
  },
}))